class Solution:
    def longestSubarray(self, nums: list, limit: int) -> int:
        minQueue = []
        maxQueue = []
        res = 0
        left = 0
        right = 0
        while right < len(nums):
            cur = nums[right]
            while minQueue and nums[minQueue[-1]] > cur:
                minQueue.pop()
            while maxQueue and nums[maxQueue[-1]] < cur:
                maxQueue.pop()
            minQueue.append(right)
            maxQueue.append(right)
            absRes = nums[maxQueue[0]] - nums[minQueue[0]]
            while absRes > limit:
                left += 1
                while left > minQueue[0]:
                    minQueue.pop(0)
                while left > maxQueue[0]:
                    maxQueue.pop(0)
                absRes = nums[maxQueue[0]] - nums[minQueue[0]]

            res = max(res, right - left + 1)
            right += 1
        return res
